-
Notifications
You must be signed in to change notification settings - Fork 2
/
wcsrtombs.c
76 lines (53 loc) · 1.69 KB
/
wcsrtombs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*++
toro C Library
https://github.com/KilianKegel/toro-C-Library#toro-c-library-formerly-known-as-torito-c-library
Copyright (c) 2017-2024, Kilian Kegel. All rights reserved.
SPDX-License-Identifier: GNU General Public License v3.0
Module Name:
wcsrtombs.c
Abstract:
Convert a wide character string to its multibyte character string representation.
Author:
Kilian Kegel
--*/
#include <CdeServices.h>
extern size_t wcslen(const wchar_t* pszBuffer);
/**
Synopsis
#include <wchar.h>
size_t wcsrtombs(char* mbstr, const wchar_t** wcstr, size_t count, mbstate_t* mbstate);
Description
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/wcsrtombs
https://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf#page=404
@param[in] *wcstr
*mbstr
count
*mbstate - ignored
@retval 1 size of converted character
@retval 0 on failure
**/
size_t wcsrtombs(char* mbstr, const wchar_t** wcstr, size_t count, mbstate_t* mbstate)
{
char c;
size_t initial_count = count;
wchar_t* pwc = (wchar_t*)*wcstr;
do {
if (NULL != wcstr && 0 == wcslen(*wcstr))
{
initial_count = 0;
break;
}
if (NULL == mbstr)
{
initial_count = NULL == wcstr ? 0 : wcslen(*wcstr); // Microsoft decided to return remaining string length if NULL == destination, not my choice!!!
break;
}
if (0 == count)
break;
c = (char)*pwc++;
if(NULL != wcstr)
*wcstr = (void*)pwc;
*mbstr++ = c;
} while ('\0' != c && --count);
return initial_count;;
}